﻿using System;
using System.Web.UI;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class UpdatePetitionNo : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_Petitions pet = new B_FIM_Petitions();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void LoadData()
        {
            M_FIM_Petitions info = pet.GetInfo(Session["PetitionSeq"].ToString());
            DateTime dateRealPetitionDate = DateTime.Today;

            if (info.real_petition_date != null)
            {
                dateRealPetitionDate = info.real_petition_date.Value;
            }

            dpRealPetitionDate.SetValue(dateRealPetitionDate);
            txtPetitionNo.Text = info.petition_no;
        }

        protected void cmdOK_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }

            M_FIM_Petitions info = new M_FIM_Petitions();
            info.petition_seq = Convert.ToInt32(Session["PetitionSeq"]);
            info.petition_no = txtPetitionNo.Text;
            info.real_petition_date = dpRealPetitionDate.GetDateValue();
            info.updated_by = main.ConvertNullInt(Session["EmpSeq"]);
            info.updated_when = DateTime.Now;

            pet.UpdatePetitionNo(info);

            main.MessageAlert(Page, "บันทึกเรียบร้อย");
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "onclick"
                , "returnToParent()", true);
        }

        protected bool CheckData()
        {
            bool bOK = true;
            string strRealPetitionDate = dpRealPetitionDate.GetStringValue();
            string strPetitionNo = txtPetitionNo.Text;

            if (strRealPetitionDate.Length == 0)
            {
                bOK = false;
                main.MessageAlert(Page, "กรุณากรอกข้อมูล [วันที่ขอเบิกในระบบ GFMIS]");
            }
            else if (strPetitionNo.Length == 0)
            {
                bOK = false;
                main.MessageAlert(Page, "กรุณากรอกข้อมูล [เลขที่ขอเบิกในระบบ GFMIS]");
            }

            return bOK;
        }
    }
}